package top.lyunk.demo.asm;

import org.objectweb.asm.ClassWriter;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 * 说明：
 * </pre>
 *
 * @author lyk
 * @date 2022/9/9
 */
public class App {
    private static List<NeedTodo> list = new ArrayList<>();
    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            // 生成子类
            NeedTodo todo = generate();

            // 放入集合
            list.add(todo);

            // 循环遍历子类
            todo();

            // 移除子类
            list.remove(todo);

            new Thread(() ->{
                sleep(3* 1000);
                System.out.println("线程2开始");

                // dump
                dump();
                // 卸载子类
                // downloadClazz();
                // dump
                dump();

                // 重新生成子类
                NeedTodo todo2 = generate();

                // dump
                dump();
                System.out.println("线程2结束");
            }).start();

            System.out.println("线程1结束");
        }).start();


        sleep(60 * 1000);
    }

    private class MyClassLoader extends ClassLoader {
        public Class<?> defineClass(String name, byte[] b)
                throws ClassFormatError
        {
            return defineClass(name, b, 0, b.length);
        }
    }

    private static NeedTodo generate() {
        ClassWriter cv = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        return null;
    }

    private static void dump() {
        System.out.println("dump");
    }

    private static void todo() {
        for (NeedTodo needTodo : list) {
            needTodo.print("a");
        }
    }

    public static void sleep(long time){
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public interface NeedTodo {
        void print(String str);
    }
}
